<?php
/**
 * Контроллер интернет-магазина административной панели
 * 
 * @category Phorm
 * @package Controller_Module_Control
 * @name /Phorm/Controller/Module/Control/Store.php
 * @copyright phorm.ru
 * @since 18.07.2012
 * @author vladimir@kachalov.net
 */

class Phorm_Controller_Module_Control_Store extends Phorm_Controller_Controller {
	
	/**
	 * Экземпляр класса интернет-магазина
	 * @var Phorm_Store
	 */
	public $Store;
	
	
	/**
	 * Предиспетчер
	 * 
	 * @return void
	 */
		
	public function preDispatch() {
		
		$this->Store = new Phorm_Store();
		
	}
	
	/**
	 * Главная страница
	 * 
	 * @return void
	 */
		
	public function indexAction() {
		
						
	}
	
	
	/**
	 * Список способов доставки
	 * 
	 * @return void
	 */
		
	public function deliveryAction() {
		
		$options = Phorm_Config::xml('grids/control/store.xml','delivery');
		
		if($this->_request->isXmlHttpRequest()) {
			
			$rows = $this->Store->getDeliveryList($this->_params);
			$paginator = $this->Store->getPaginator();

			$this->_helper->json(Phorm_JQuery_DataGrid::data($rows,$paginator,$options));
			
		} else {
			
			$this->view->assign('GridDataTable',$this->view->DataGrid('GridDataTable',$options));
			
		}
		
						
	}
	
	
	/**
	 * Добавление способа доставки
	 * 
	 * @return void
	 */
		
	public function adddeliveryAction() {
		
		$form = $this->Store->getDeliveryForm('adddelivery');
		
		if($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
			
			if($this->Store->saveDeliveryForm($form)) {
				return $this->composeSuccessMessage('Способ доставки сохранен','/control/store/delivery/');
			} else {
				$this->composeTransactionErrorMessage($this->Store->_errors);
			}
			
		}
		
		$this->view->assign('Form',$form);
						
	}
	
	
	/**
	 * Редактирование способа доставки
	 * 
	 * @return void
	 */
		
	public function editdeliveryAction() {
		
		if(isset($this->_params['deliveryid']) && $DeliveryInfo = $this->Store->getDeliveryInfo($this->_params['deliveryid'])) {
			
			$form = $this->Store->getClientgroupForm('editdelivery');
			$form->setDefaults($DeliveryInfo);
		
			if($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
				
				if($this->Store->saveDeliveryForm($form,$DeliveryInfo['deliveryid'])) {
					return $this->composeSuccessMessage('Изменения внесены','/control/store/delivery/');
				} else {
					$this->composeTransactionErrorMessage($this->Store->_errors);
				}
				
			}
			
			$this->view->assign('Form',$form);
			
		} else {
			return $this->composeErrorMessage('Способ доставки с указанным Id не найден','/control/store/delivery/');
		}
						
	}
	
	
	/**
	 * Удаление способа доставки
	 * 
	 * @return void
	 */
		
	public function deletedeliveryAction() {
		
		if(isset($this->_params['deliveryid']) && $DeliveryInfo = $this->Store->getDeliveryInfo($this->_params['deliveryid'])) {
			
			if($this->Store->deleteResource('delivery',$DeliveryInfo['deliveryid'])) {
				return $this->composeSuccessMessage('Выбранный способ доставки удален');
			} else {
				return $this->composeDeleteErrorMessage($this->Store->_errors);
			}
			
		} else {
			return $this->composeErrorMessage('Способ доставки с указанным Id не найден','/control/store/delivery/');
		}
						
	}
	
	
	/**
	 * Список товарных групп
	 * 
	 * @return void
	 */
		
	public function itemgroupsAction() {
		
		$options = Phorm_Config::xml('grids/control/store.xml','itemgroups');
		
		if($this->_request->isXmlHttpRequest()) {
			
			$rows = $this->Store->getItemgroupsList($this->_params);
			$paginator = $this->Store->getPaginator();

			$this->_helper->json(Phorm_JQuery_DataGrid::data($rows,$paginator,$options));
			
		} else {
			
			$this->view->assign('GridDataTable',$this->view->DataGrid('GridDataTable',$options));
			
		}
						
	}
	
	
	/**
	 * Добавление товарной группы
	 * 
	 * @return void
	 */
		
	public function additemgroupAction() {
		
		$form = $this->Store->getItemgroupForm('additemgroup');
		
		if($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
			
			if($this->Store->saveItemgroupForm($form)) {
				return $this->composeSuccessMessage('Товарная группа сохранена','/control/store/itemgroups/');
			} else {
				$this->composeTransactionErrorMessage($this->Store->_errors);
			}
			
		}
		
		$this->view->assign('Form',$form);
						
	}
	
	
	/**
	 * Редактирование товарной группы
	 * 
	 * @return void
	 */
		
	public function edititemgroupAction() {
		
		if(isset($this->_params['itemgroupid']) && $GroupInfo = $this->Store->getItemgroupInfo($this->_params['itemgroupid'])) {
			
			$form = $this->Store->getItemgroupForm('edititemgroup');
			$form->setDefaults($GroupInfo);
		
			if($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
				
				if($this->Store->saveItemgroupForm($form,$GroupInfo['itemgroupid'])) {
					return $this->composeSuccessMessage('Изменения внесены','/control/store/itemgroups/');
				} else {
					$this->composeTransactionErrorMessage($this->Store->_errors);
				}
				
			}
			
			$this->view->assign('Form',$form);
			
		} else {
			return $this->composeErrorMessage('Товарная группа с указанным Id не найдена','/control/store/itemgroups/');
		}
						
	}
	
	
	/**
	 * Удаление товарной группы
	 * 
	 * @return void
	 */
		
	public function deleteitemgroupAction() {
		
		if(isset($this->_params['itemgroupid']) && $GroupInfo = $this->Store->getItemgroupInfo($this->_params['itemgroupid'])) {
			
			if($this->Store->deleteResource('itemgroups',$GroupInfo['itemgroupid'])) {
				return $this->composeSuccessMessage('Выбранная товарная группа удалена');
			} else {
				return $this->composeDeleteErrorMessage($this->Store->_errors);
			}
			
		} else {
			return $this->composeErrorMessage('Товарная группа с указанным Id не найдена','/control/store/itemgroups/');
		}
						
	}
	
	
	/**
	 * Список типов клиентов
	 * 
	 * @return void
	 * @todo Перевести все гриды на compose-метод аля composeDataGrid($gridname,$gridoptions,$model,$methodname)
	 */
		
	public function clientgroupsAction() {
		
		$options = Phorm_Config::xml('grids/control/store.xml','clientgroups');
		
		if($this->_request->isXmlHttpRequest()) {
			
			$rows = $this->Store->getClientgroupsList($this->_params);
			$paginator = $this->Store->getPaginator();

			$this->_helper->json(Phorm_JQuery_DataGrid::data($rows,$paginator,$options));
			
		} else {
			
			$this->view->assign('GridDataTable',$this->view->DataGrid('GridDataTable',$options));
			
		}
						
	}
	
	
	/**
	 * Добавление типа клиентов
	 * 
	 * @return void
	 */
		
	public function addclientgroupAction() {
		
		$form = $this->Store->getClientgroupForm('addclientgroup');
		
		if($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
			
			if($this->Store->saveClientgroupForm($form)) {
				return $this->composeSuccessMessage('Тип клиента сохранен','/control/store/clientgroups/');
			} else {
				$this->composeTransactionErrorMessage($this->Store->_errors);
			}
			
		}
		
		$this->view->assign('Form',$form);
						
	}
	
	
	/**
	 * Редактирование типа клиентов
	 * 
	 * @return void
	 */
		
	public function editclientgroupAction() {
		
		if(isset($this->_params['clientgroupid']) && $GroupInfo = $this->Store->getClientgroupInfo($this->_params['clientgroupid'])) {
			
			$form = $this->Store->getClientgroupForm('editclientgroup');
			$form->setDefaults($GroupInfo);
		
			if($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
				
				if($this->Store->saveClientgroupForm($form,$GroupInfo['clientgroupid'])) {
					return $this->composeSuccessMessage('Изменения внесены','/control/store/clientgroups/');
				} else {
					$this->composeTransactionErrorMessage($this->Store->_errors);
				}
				
			}
			
			$this->view->assign('Form',$form);
			
		} else {
			return $this->composeErrorMessage('Тип клиента с указанным Id не найден','/control/store/clientgroups/');
		}
						
	}
	
	
	/**
	 * Удаление типа клиентов
	 * 
	 * @return void
	 */
		
	public function deleteclientgroupAction() {
		
		if(isset($this->_params['clientgroupid']) && $GroupInfo = $this->Store->getClientgroupInfo($this->_params['clientgroupid'])) {
			
			if($this->Store->deleteResource('clientgroups',$GroupInfo['clientgroupid'])) {
				return $this->composeSuccessMessage('Выбранный тип клиента удален');
			} else {
				return $this->composeDeleteErrorMessage($this->Store->_errors);
			}
			
		} else {
			return $this->composeErrorMessage('Тип клиента с указанным Id не найден','/control/store/clientgroups/');
		}
						
	}

	
	
}